import { ref } from "vue"
import { postCommentReply } from "@/api/dynamic"

// 评论
export const useReply = form => {
  let topId = 0
  let reCommId = 0
  let content = ref('')
  let placeholder = ref('写评论')
  let focus = ref(false)
  let show = ref(false)
  let bottom = ref(0)
  let callBack = null
  let replyFocus = event => {
    bottom.value = event.detail.height
  }
  let onOpenReply = (firstId = 0, secondId = 0, callback = () => { }) => {
    callBack = callback
    topId = firstId
    reCommId = secondId
    show.value = true
    setTimeout(() => {
      focus.value = true
    }, 150)
  }
  let onReply = async level => {
    let { reprId, replyType } = form
    const { code, data: info } = await postCommentReply({ content: content.value, reprId, replyType, topId, reCommId })
    if (code === 200) {
      if (typeof level === 'function') {
        level(info)
      } else {
        callBack(info)
      }
      onCloseReply()
      content.value = ''
    }
  }
  // let onReply = async callBack => {
  //   let { reprId, replyType } = form
  //   const { code, data: info } = await postCommentReply({ content: content.value, reprId, replyType, topId, reCommId })
  //   if (code === 200) {
  //     if (typeof callBack === "function") {
  //       callBack(info)
  //     }
  //     onCloseReply()
  //     content.value = ''
  //   }
  // }
  let onCloseReply = () => {
    focus.value = false
    show.value = false
  }
  return { content, placeholder, focus, show, bottom, replyFocus, onReply, onOpenReply, onCloseReply }
}
